package com.tsfot.spider.analyze;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tsfot.spider.adapter.URLAdapter;
import com.tsfot.spider.network.NetworkService;
import com.tsfot.spider.service.DataCollectionService;
import com.tsfot.spider.task.SpiderTask;
import com.tsfot.spider.task.Task;



/**
 * 解析首页服务
 * @author frank0
 *
 */
public class HomeAnalyzeService extends AbstractAnalyzeService{

	private final static Logger logger=LoggerFactory.getLogger(HomeAnalyzeService.class);
	
	private final static int COMPUTER_THREAD_COUNT=10;
	
	private NetworkService networkService;
	
	private LinkedBlockingQueue<Task> pageListQueue = null;
	
	protected SpiderTask doAnalysis(Task task) {
		SpiderTask resultTask=new SpiderTask();
		String name=config.getProjectName()+"_index";
		Task download = networkService.download(config.getQueryUrl(),name);
		//读取 进行分析
		String content = fileService.readFileToString(download.getLocalPath());
		Document doc = Jsoup.parse(content);
		Elements paginates = doc.select(config.getPaginateCssQuery());
		if(paginates!=null&&config.getTotalPage()==0){
			resultTask.setTotalPage(paginates.size());
		}else{
			resultTask.setTotalPage(config.getTotalPage());
		}
		return resultTask;
	}

	public void start() {
		logger.info("启动首页分析服务....");
		ExecutorService fixedThreadPool = Executors.newFixedThreadPool(COMPUTER_THREAD_COUNT);
		//解析首页
		SpiderTask spiderTask =this.doAnalysis(null);
		int totalPage = spiderTask.getTotalPage();
		for (int i = 1; i <= totalPage; i++) {
			SpiderTask task=new SpiderTask(i);
			task.setFileName(config.getProjectName()+"_index_"+i);
			task.setUrl(config.getQueryUrl());
			task.setUrl(this.adapter.adapter(task));
			fixedThreadPool.execute(new HomeAnalyzeWorker(task));
		}
		fixedThreadPool.shutdown();
		logger.info("首页分析完成....");
	}

	public void stop() {
		
	}
	
	private class HomeAnalyzeWorker implements Runnable {
		
        private Task task;

        HomeAnalyzeWorker(Task task) {
        	this.task = task;
        }

        public void run() {
        	runInBackground(task);
        }
    }
	
	private void runInBackground(Task task) {
		//下载到本地，然后进行解析，生成详情任务，把任务放入到详情队列中
		SpiderTask lpTask=(SpiderTask)task;
		String name=config.getProjectName()+"_list_"+lpTask.getPageNumber();
		Task listPageTask= networkService.download(lpTask.getUrl(),name);
		listPageTask.setPageNumber(lpTask.getPageNumber());
		try {
			pageListQueue.put(listPageTask);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public Task analysis(Task task) {
		// TODO Auto-generated method stub
		return null;
	}

	public void setNetworkService(NetworkService networkService) {
		this.networkService = networkService;
	}

	public void setPageListQueue(LinkedBlockingQueue<Task> pageListQueue) {
		this.pageListQueue = pageListQueue;
	}

}
